package com.wwj.feedback.schudule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wwj.feedback.constant.ConstantFinal;
import com.wwj.feedback.entitiy.FeedbackQuestionEntity;
import com.wwj.feedback.service.FeedbackQuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Slf4j
@EnableScheduling
@EnableAsync
@Service
public class FeedbackSchedule {

    @Resource
    FeedbackQuestionService feedbackQuestionService;

    private final long day3Gap = 1000 * 60 * 60 * 72;

    /**
     * 三天之后删除完结的数据的定时任务
     */
    @Async
    @Scheduled(cron = "0 0 0 1/1 * ?") // 每日执行一次
    void handleDeleteInformationAfter3Day(){
        log.info("==========执行定时任务==========");
        QueryWrapper<FeedbackQuestionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ConstantFinal.FIELD_QUESTION_IDENTIFY, ConstantFinal.VALUE_QUESTION_IDENTIFY_ANSWER);
        List<FeedbackQuestionEntity> list = feedbackQuestionService.list(queryWrapper);
        long nowTime = new Date().getTime();
        list.forEach(item -> {
            if (item.getFinishTime() != null){
                long createTime = item.getFinishTime().getTime();
                // 检测如果大于了三天执行删除
                if (nowTime - createTime > day3Gap) {
                    feedbackQuestionService.list();
                    feedbackQuestionService.removeById(item.getQuestionId());
                }
            }
        });
        log.info("==========定时任务结束==========");
    }
}
